<?php
namespace backend\models;
use Yii;
use common\tools\HcUploadFile;
use \yii\db\ActiveRecord;

class Image extends ActiveRecord{

    public static function tableName(){
        return '{{%image}}';
    }

    public function rules(){
        return [
            ['thumb', 'file', 'extensions' => ['png', 'jpg', 'gif', 'jpeg'], 'maxSize' => 1024*1024*1024],
            ['title', 'required', 'message' => '图片标题不能为空']
        ];
    }

    public function attributeLabels(){
        return [
            'title' => '图片标题',
            'thumb' => '选择图片',
        ];
    }

    public function beforeSave($insert)
    {
        $uploadedFile=HcUploadFile::uploadFiles("Image[thumb]");
        if($uploadedFile!=null){
            $this->thumb = $uploadedFile['url'].$uploadedFile['new_name'];
        }
        return parent::beforeSave($insert);
    }
}